package com.nexttwist;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.widget.Toast;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import com.nexttwist.auth.AuthenticationHandler;
import com.nexttwist.util.SystemUiHider;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 * 
 * @see SystemUiHider
 */
public class Splash extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_slpash);
		// startActivity(new Intent(this, HomeActivity.class));
		/*
		 * Context mContext = BaseApp.getInstance().getApplicationContext();
		 * System.out.println(mContext);
		 */
		// finish();
		/*try {
			PackageInfo info = getPackageManager().getPackageInfo("com.nexttwist", PackageManager.GET_SIGNATURES);
			for (Signature signature : info.signatures) {
				MessageDigest md = MessageDigest.getInstance("SHA");
				md.update(signature.toByteArray());
				Log.d("KeyHash:",
						Base64.encodeToString(md.digest(), Base64.DEFAULT));
			}
		} catch (NameNotFoundException e) {

		} catch (NoSuchAlgorithmException e) {

		}*/

		// start Facebook Login
		Session.openActiveSession(this, true, new Session.StatusCallback() {

			// callback when session changes state
			@Override
			public void call(Session session, SessionState state,
					Exception exception) {
				if (session.isOpened()) {
					//Toast.makeText(getApplicationContext(), "Session Openned ", Toast.LENGTH_LONG).show();
					// make request to the /me API
					Request.newMeRequest(session,
							new Request.GraphUserCallback() {
								// callback after Graph API response with user
								// object
								@Override
								public void onCompleted(GraphUser user,
										Response response) {
									if (user != null) {
										Toast.makeText(getApplicationContext(), "Hello " + user.getName(), Toast.LENGTH_LONG).show();
										AuthenticationHandler.getInstance().setmGraphUser(user);
										startActivity(new Intent(Splash.this, HomeActivity.class));
										finish();
									}
								}
							}).executeAsync();
				} else {
					Toast.makeText(getApplicationContext(), "Not Connected Please Check Internet", Toast.LENGTH_LONG).show();
					finish();
				}
				
			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Session.getActiveSession().onActivityResult(this, requestCode,
				resultCode, data);
	}

}
